package com.demo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.hcvsa.MyHCVSA;
import com.hcvsa.UserHandUpCall;
import com.squareup.otto.Subscribe;
import com.view.MyVideoRel2;

import butterknife.BindView;
import butterknife.OnClick;
import fun.flyee.android.pengshan.acts.BaseActivity;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.pengshan.social.R;
import hcvs.hcvsa.MeetingEvent;


public class MeetingActivity extends BaseActivity {

    public static final String TAG = "MeetingActivity";

    private int meetingId;

    @BindView(R.id.myVideoRel)
    public MyVideoRel2 myVideoRel;

    @Override
    protected int layoutId() {
        return R.layout.activity_meeting;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕不息屏
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);//点亮屏幕
        Systems.hideSystemNavBar(this);
        init();

        /* ------------注意-------------  */
        /*  demo未进行动态权限申请，使用时请确保应用具备相关权限 */
    }

    @Override
    public void onDestroy() {
        uninit();
        super.onDestroy();
    }

    private void init() {
        Intent intent = getIntent();
        meetingId = intent.getIntExtra("meetingid", 0);
        meetingId = 2;//测试会议室用2

        //加入会议室
        if (meetingId > 0) {
            MyHCVSA.getInstance().getMeetingMoudle().JoinMeetingRoom(meetingId, "");
        }

        //打开我的摄像头
        boolean isOK = MyHCVSA.getInstance().OpenMyVideoChannel2(0);

    }

    @OnClick(R.id.switch_camera_btn)
    public void switchCamera() {
        myVideoRel.getCameraSurface(0).cameraFacingChanged();
    }

    @OnClick(R.id.hangup_btn)
    public void hangup() {
        uninit();
        finish();
    }

    @Subscribe
    public void UserHandUpCall(UserHandUpCall data) {
        uninit();
        Toast.makeText(this, "会议结束", Toast.LENGTH_LONG).show();
        finish();
    }

    private void uninit() {
        //关闭我的摄像头
        MyHCVSA.getInstance().CloseMyVideoChannel2(0);
        //退出会议室
        MyHCVSA.getInstance().getMeetingMoudle().ExitMeetingRoom();
        //释放资源
        if (myVideoRel != null) {
            try {
                myVideoRel.release();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

}
